﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IGuess
{
    public class FrequenceClassifier : Classifier
    {
        //Atributes
        string mostOccurentWord;
        int numberOfWords=1;
        private System.Resources.ResourceManager RM = new System.Resources.ResourceManager("IGuess.GlobalResources", System.Reflection.Assembly.GetExecutingAssembly());
        private string result;
        
        //Constructors
        public FrequenceClassifier(string text)
        {
            this.text = text;
        }

        public FrequenceClassifier() { }

        //Properties
        public string MostOccurentWord
        {
            get { return this.mostOccurentWord; }
            set { this.mostOccurentWord = value; }
        }

        public string Result
        {
            get { return this.result; }
            set { this.result = value; }
        }

        public int NumberOfWords
        {
            get { return this.numberOfWords; }
            set { this.numberOfWords = value; }
        }

        //Methods
        public override string Classify()
        {

            // calculate number of words in the text
            this.wordsInAText(text);
            this.mostFrequentWord(text);

            #region English stuff

            int nA = CountStringOccurrences(this.Text, "a");
            int nO = CountStringOccurrences(this.Text, "o");
            int nT = CountStringOccurrences(this.Text, "t");

            Decimal fA = Decimal.Divide(nA, numberOfWords);
            Decimal fO = Decimal.Divide(nO, numberOfWords);
            Decimal fT = Decimal.Divide(nT, numberOfWords);

            #endregion

            #region french stuff

            int nI = CountStringOccurrences(this.Text, "i");
            int nS = CountStringOccurrences(this.Text, "s");

            Decimal fI = Decimal.Divide(nI, numberOfWords);
            Decimal fS = Decimal.Divide(nS, numberOfWords);

            #endregion

            #region German stuff

            int nE = CountStringOccurrences(this.Text, "e");
            int nN = CountStringOccurrences(this.Text, "n");
            int nR = CountStringOccurrences(this.Text, "r");

            Decimal fE = Decimal.Divide(nE, numberOfWords);
            Decimal fN = Decimal.Divide(nN, numberOfWords);
            Decimal fR = Decimal.Divide(nR, numberOfWords);

            #endregion

            #region Scores

            Decimal DEScore = fE + fN + fR;
            Decimal FRScore = fI + fS;
            Decimal ENScore = fA + fO + fT;

            #endregion

            if (DEScore > FRScore && DEScore > ENScore)
            {
                this.Result = "DE";
                return RM.GetString("FrequenceAlgorithmResult") + RM.GetString("DETextLanguage") + ".";
            }
            else if (FRScore > DEScore && FRScore > ENScore)
            {
                this.Result = "EN";
                return RM.GetString("FrequenceAlgorithmResult") + RM.GetString("FRTextLanguage") + ".";
            }
            else if (ENScore > DEScore && ENScore > FRScore)
            {
                this.Result = "FR";
                return RM.GetString("FrequenceAlgorithmResult") + RM.GetString("ENTextLanguage") + ".";
            }
            else
            {
                this.Result = "NA";
                return RM.GetString("FrequenceAlgorithmUnknownResult");
            }

        }

        public int CountStringOccurrences(string text, string pattern)
        {
            // Loop through all instances of the string 'text'.
            int count = 0;
            int i = 0;
            while ((i = text.ToLower().IndexOf(pattern.ToLower(), i)) != -1)
            {
                i += pattern.Length;
                count++;
            }
            return count;
        }

        private void wordsInAText(string text)
        {

            char[] delimit = new char[] { ' ' };
            string s10 = text;

            this.numberOfWords = s10.Split(delimit).Length;
        }

        private void mostFrequentWord(string text)
        {

            string[] words = text.Split(' ', ',', '.', '?', '!', '"');

            var frequencyOrder = from word in words
                                 group word by word into g
                                 orderby g.Count() descending
                                 select g.Key;

            var commonWords = frequencyOrder.Take(1);

            this.mostOccurentWord = commonWords.First().ToString();

        }
    }
}
